package com.shundr.shipper.truck;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.MyDatePickerDialog;
import com.shundr.shipper.common.view.StatementViewPager;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.http.UserTruckRequest;
import com.shundr.shipper.truck.model.UserTrackInfo;

/**
 * 
 * UserTruckMapActivity
 * 
 * @author larry 2015-6-27 下午2:08:08 
 * @version 1.0.0
 *
 */
public class UserTruckMapActivity extends BaseActivity implements OnClickListener{
	
	private StatementViewPager mViewPager;// ViewPager对象的引用

	private List<Fragment> mFragmentList;// 装载Fragment的容器，我们的每一个界面都是一个Fragment
	private UserTruckTimeFragment mTimeFragment;
	private UserTruckMapFragment mMapFragment;

	private FragmentPagerAdapter mAdapter;
	
	private Button btnStart;
	private Button btnEnd;
	private Button btnSearch;
	
	private String start;
	private String end;
	
	private int userId;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UserTruckRequest.GET_USER_TRACK_LIST_SUCCESSED:
				@SuppressWarnings("unchecked")
				List<UserTrackInfo> infos = (List<UserTrackInfo>)msg.obj;
				if (infos != null) {
					List<UserTrackInfo> list = new ArrayList<UserTrackInfo>();
					for(UserTrackInfo info : infos){
						if(info.getLocationLat() != info.getLocationLng()){
							if(info.getLocationLat() > info.getLocationLng()){
								double lat = info.getLocationLng();
								double lng = info.getLocationLat();
								info.setLocationLat(lat);
								info.setLocationLng(lng);
								list.add(info);
							}else{
								list.add(info);
							}
						}
					}
					mTimeFragment.refreshTimeData(list);
					mMapFragment.refreshMapData(list);
				}
				ProgressDialogHelper.dismiss();
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar.setDisplayShowHomeEnabled(false);
		setContentView(R.layout.activity_user_truck_map);
		userId = getIntent().getIntExtra("userId", -1);
		String title = getIntent().getStringExtra("title");
		setTitle(title);
		if(userId == -1){
			finish();
			return;
		}
		start = DateUtil.getNowTime();
		end = DateUtil.getNowTime();
		initView();
		initData();
	}

	private void initView() {
		
		btnStart = (Button)findViewById(R.id.btn_start);
		btnEnd = (Button)findViewById(R.id.btn_end);
		btnSearch = (Button)findViewById(R.id.btn_search);
		btnStart.setOnClickListener(this);
		btnEnd.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
		
		btnStart.setText(DateUtil.toDate_1(start));
		btnEnd.setText(DateUtil.toDate_1(end));
		
		mFragmentList = new ArrayList<Fragment>();
		mTimeFragment = new UserTruckTimeFragment();
		mMapFragment = new UserTruckMapFragment();
		mFragmentList.add(mTimeFragment);
		mFragmentList.add(mMapFragment);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mFragmentList.size();
			}

			@Override
			public Fragment getItem(int position) {
				return mFragmentList.get(position);
			}
		};

		mViewPager = (StatementViewPager) findViewById(R.id.viewPager);
		// 设置Adapter
		mViewPager.setAdapter(mAdapter);
		// 设置监听
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}
	
	private void initData() {
		ProgressDialogHelper.show(mContext);
		userId = 2;
		new UserTruckRequest(mContext, mHandler).getTruckMap(userId, 0, start, end);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		menu.getItem(0).setTitle("地图");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			if(mViewPager.getCurrentItem() == 0){
				mViewPager.setCurrentItem(1,false);
				item.setTitle("列表");
			}else{
				mViewPager.setCurrentItem(0,false);
				item.setTitle("地图");
			}
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		Calendar cal = Calendar.getInstance();
		switch (v.getId()) {
		case R.id.btn_start:
			MyDatePickerDialog startDialog = new MyDatePickerDialog(mContext,
					new OnDateSetListener() {

						@Override
						public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
							btnStart.setText(year + "-" + StringUtil.getDateStr((monthOfYear + 1)) + "-" + StringUtil.getDateStr(dayOfMonth));
							start = DateUtil.toFormatDate(btnStart.getText().toString());
						}
					}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			startDialog.show();
			startDialog.initView(true, true, true);
			break;
		case R.id.btn_end:
			MyDatePickerDialog endDialog = new MyDatePickerDialog(mContext,
					new OnDateSetListener() {

						@Override
						public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
							btnEnd.setText(year + "-" + StringUtil.getDateStr((monthOfYear + 1)) + "-" + StringUtil.getDateStr(dayOfMonth));
							end = DateUtil.toFormatDate(btnEnd.getText().toString());
						}
					}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			endDialog.show();
			endDialog.initView(true, true, true);
			break;
		case R.id.btn_search:
			initData();
			break;

		default:
			break;
		}
	}
	
}
